package main

import (
	"fmt"
	"github.com/spf13/viper"
	"google.golang.org/grpc"
	"log"
	"net"
	"os"
	"uploadServer/config"
	uploadpb "uploadServer/protos"
	"uploadServer/server"
)

func main() {

	// 加载系统环境变量
	env := os.Getenv("GO_ENV")
	if env == "" {
		env = "development"
	}

	// 初始化配置，指定配置路径（这里假设当前目录）
	err := config.InitConfig(env, "./config")
	if err != nil {
		log.Fatalf("配置初始化失败: %v", err)
	}

	// 从配置中读取gRPC服务器端口
	port := viper.GetInt("app.grpc_port")
	if port == 0 {
		port = 50051 // 默认端口
	}

	// 启动gRPC服务器
	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}

	s := grpc.NewServer()
	uploadServer := server.NewUploadServer()
	uploadpb.RegisterUploadServiceServer(s, uploadServer)

	log.Printf("gRPC server starting on port %d...", port)
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
